/*  BEGIN_FILE_HDR
******************************************Copyright(C)*****************************************
*
*                                       YKXH  Technology
*
***********************************File Info***************************************************
*   File Name       @: OsekNm.h
************************************************************************************************
*   Project/Product @:
*   Title           @:
*   Author          @: donglin.Zhang
************************************************************************************************
*   Description     @:
*
************************************************************************************************z
*   Limitations     @: None
*
************************************************************************************************
*
************************************************************************************************
*   Revision History:
*
*    Version    Date          Initials       CR#             Descriptions
*   ---------   ----------    -------------  ----------    ---------------
*   1.0         2018/05/30    donglin.zhang   N/A          Original
*   1.1         2019/04/01    zhongliang.Yang N/A          OsekNm190401-01
*
************************************************************************************************
* END_FILE_HDR*/

#ifndef _OSEKNM_H_
#define _OSEKNM_H_


/*******************************************************************************
*   Includes 
*******************************************************************************/
#include "../src/BSW/CommS/NM/NM/OSEK/OsekNm_Cfg.h"
#include "..\src\BSW\Comms\NM\YKBase\BaseComm.h"


/*******************************************************************************
* Global macro defines
*******************************************************************************/
#define DATA_INIT        0x00u
#define NM_BIT_SET       0x01u
#define NM_BIT_CLEAR     0x00u

/*******************************************************************************
Local macro defines
*******************************************************************************/
/* configuration needed :relate to Licence */
#define V_DEF_SUPPORTED_PLATFORM
#define C_COMP_DIABDATA_MPC55XX
#define NM_TYPE_YKXH
    
/*******************************************************************************
* macros for variable access
*******************************************************************************/
#define NM_INVALID_CHANNEL                0xFFu
/* NM State Code Mask */
/* mask: 00011111 */
#define NM_STATE_CODE_MASK                0x1Fu  
/* mask: 11100000 */
#define NM_STATE_DECODE_MASK              0xE0u   

/*NM State start*/
#define NM_STATE_CODE_NORMAL              0x00u
#define NM_STATE_CODE_LIMPHOME            0x01u
#define NM_STATE_CODE_NORMALPREPSLEEP     0x02u
#define NM_STATE_CODE_LIMPHOMEPREPSLEEP   0x03u
#define NM_STATE_CODE_TWBSNORMAL          0x04u
#define NM_STATE_CODE_TWBSLIMPHOME        0x05u
#define NM_STATE_CODE_BUSSLEEP            0x08u
#define NM_STATE_CODE_OFF                 0x10u
/*NM State end*/
#define NM_OWN_STATION nmNodeChannelID[nmCurChannel]
#define NMTXPDUID nmTxPduIdAry[nmCurChannel]
#define nmStatus nmStatusAry[nmCurChannel]
#define nmTxDataFld nmTxDataFldAry[nmCurChannel]
#define nmRxDataFld nmRxDataFldAry[nmCurChannel]
#define nmLogicalSuccessor nmLogicalSuccessorAry[nmCurChannel]
#define nmRxMsgId nmRxMsgIdAry[nmCurChannel]
#define nmRxCount nmRxCountAry[nmCurChannel]
#define nmTxCount nmTxCountAry[nmCurChannel]
#define nmTimerCntTtyp nmTimerCntTtypAry[nmCurChannel]  
#define nmTimerCntTmax nmTimerCntTmaxAry[nmCurChannel]
#define nmTimerCntTerr nmTimerCntTerrAry[nmCurChannel]
#define nmTimerCntTwbs nmTimerCntTwbsAry[nmCurChannel]
#define nmTimerCntTtx nmTimerCntTtxAry[nmCurChannel]
#define nmTimerCntTbusoff nmTimerCntTbusoffAry[nmCurChannel]
#if(T_ACTIVE_MINI_ON == STD_ON)
#define nmTimerCntTactivemini nmTimerCntTactiveminiAry[nmCurChannel]             /*ziliang_debug*/
#endif
#if(T_LIMPHOME_MINI_ON == STD_ON)
#define nmTimerCntTlimphome nmTimerCntTlimphomeAry[nmCurChannel]                 /*ziliang_debug*/
#endif
#define nmCurState nmCurStateAry[nmCurChannel]
#define nmCanBusOffFlg nmCanBusOffFlgAry[nmCurChannel]
#define nmCanBusOffBeing nmCanBusOffBeingAry[nmCurChannel]
#define nubNmBusOffCnt nubNmBusOffCntAry[nmCurChannel]
#define nmConfigNormal(x) nmConfigNormalAry[nmCurChannel][x]

#define NM_PDU_DLC               0x08
#define NM_PDU_BASICID           nmPduBasicIDAry[nmCurChannel]
#define NM_PDU_BASICID_MASK      nmPduBasicIDMaskAry[nmCurChannel]             
#define NM_PDU_SOUCEID_MASK      nmPduSouceIDMaskAry[nmCurChannel]              

#define NM_COUNT_TYP            \
    ((nmTimerCntType)(nmCountTypAry[nmCurChannel]/NM_MAINFUNCTION_PERIOD))
#define NM_COUNT_MAX            \
    ((nmTimerCntType)(nmCountMaxAry[nmCurChannel]/NM_MAINFUNCTION_PERIOD))                    
#define NM_COUNT_ERR            \
    ((nmTimerCntType)(nmCountErrAry[nmCurChannel]/NM_MAINFUNCTION_PERIOD))                    
#define NM_COUNT_WBS            \
    ((nmTimerCntType)(nmCountWbsAry[nmCurChannel]/NM_MAINFUNCTION_PERIOD))                    
#define NM_COUNT_TX             \
    ((nmTimerCntType)(nmCountTxAry[nmCurChannel]/NM_MAINFUNCTION_PERIOD))                    
#define NM_COUNT_FAST_TBUSOFF   \
    ((nmTimerCntType)(nmCountFastBusoffAry[nmCurChannel]/NM_MAINFUNCTION_PERIOD))            
#define NM_COUNT_SLOW_TBUSOFF   \
    ((nmTimerCntType)(nmCountSlowBusoffAry[nmCurChannel]/NM_MAINFUNCTION_PERIOD))
#if(T_ACTIVE_MINI_ON == STD_ON)
#define NM_COUNT_TACTIVEMINI   \
    ((nmTimerCntType)(nmCountActMinAry[nmCurChannel]/NM_MAINFUNCTION_PERIOD))       /*ziliang_debug*/
#endif
#if(T_LIMPHOME_MINI_ON == STD_ON)	
#define NM_COUNT_TLIMPHOME   \
    ((nmTimerCntType)(nmCountTLimpHAry[nmCurChannel]/NM_MAINFUNCTION_PERIOD))         /*ziliang_debug*/
#endif
#define NM_BUSOFF_FAST_RECOVERY_TIMES     nmBusOffFastToSlowAry[nmCurChannel]            
#define NM_BUSOFF_SLOW_RECOVERY_TIMES     nmBusOffSlowRecoveryAry[nmCurChannel]    
#define RX_LIMIT                nmRxLimitAry[nmCurChannel]    
#define TX_LIMIT                nmTxLimitAry[nmCurChannel]   

/*CAN ID mode : standrd or extended*/
#define NM_CAN_ID_STANDARD_ENABLE     \
    nmChannelConfigAry[nmCurChannel].B.NMIDStandardEn 
/* remote wakeup signal */
#define NM_REMOTE_WAKEUP_ENABLE       \
    nmChannelConfigAry[nmCurChannel].B.NMRemoteWakeUpEn 
/* use Ring Data */
#define NM_USERINGDATA_ENABLE         \
    nmChannelConfigAry[nmCurChannel].B.NMUseringDataEn   
/* Congfig functions */
#define NM_CONFIG_ENABLE              \
    nmChannelConfigAry[nmCurChannel].B.NMConfigEn  
/* Extended Callback functions */
#define NM_EXTENDED_CALLBACK_ENABLE   \
    nmChannelConfigAry[nmCurChannel].B.NMExtCallBackEn    


#if defined ( NM_TYPE_YKXH )
/* nmStatus */
#define nmStatusByteGlobal              nmStatus.R[0]
#define nmStatusNMCfgStable             nmStatus.B.NMCfgStable
#define nmStatusNMBusError              nmStatus.B.NMBusError
#define nmStatusNMActive                nmStatus.B.NMActive
#define nmStatusNMLimpHome              nmStatus.B.NMLimpHome
#define nmStatusNMPrepSleep             nmStatus.B.NMPrepSleep
#define nmStatusNMTwbs                  nmStatus.B.NMTwbs
#define nmStatusNMBusSleep              nmStatus.B.NMBusSleep
#define nmStatusNMOff                   nmStatus.B.NMOff

#define nmStatusByteLocal               nmStatus.R[1]
#define nmStatusLocalSleepInd           nmStatus.B.LocalSleepInd
#define nmStatusLocalSleepAck           nmStatus.B.LocalSleepAck
#define nmStatusMerkerStable            nmStatus.B.MerkerStable
#define nmStatusMerkerLimpHome          nmStatus.B.MerkerLimpHome
#define nmStatusImSkipped               nmStatus.B.ImSkipped
#define nmStatusImDestined              nmStatus.B.ImDestined
#define nmStatusUseRingData             nmStatus.B.UseRingData
#define nmStatusTransmitFlag            nmStatus.B.TransmitFlag

#define nmStatusByteLocal1              nmStatus.R[2]
#define nmStatusActTimeReach            nmStatus.B.ActTimeReach
#define nmStatusAliveSended             nmStatus.B.AliveSended
#define nmStatusLHTimeReach             nmStatus.B.LHTimeReach
#define nmStatusLHtoBusSleep            nmStatus.B.LHtoBusSleep
/* About two important algorithms */
/* Skipped in the logical ring & Determine logical successor */
#define nmRxMsgSourceId_S               (uint8)(nmRxMsgId & NM_PDU_SOUCEID_MASK)
#define nmRxMsgDestId_D                 nmRxDataFld.B.DestID
#define nmReceiverId_R                  NM_OWN_STATION
#define nmLogSucId_L                    nmLogicalSuccessor

/* nmRxDataFld */
#define nmRxDataFldOpCodeMsgType        nmRxDataFld.B.OpCodeMsgType
#define nmRxDataFldOpCodeSleepInd       nmRxDataFld.B.OpCodeSleepInd
#define nmRxDataFldOpCodeSleepAck       nmRxDataFld.B.OpCodeSleepAck

#define nmRxDataFldByteDestID           nmRxDataFld.B.DestID
#define nmRxDataFldByte0                nmRxDataFld.R[0]
#define nmRxDataFldByte1                nmRxDataFld.R[1]
#define nmRxDataFldByteUser0            nmRxDataFld.R[2]
#define nmRxDataFldByteUser1            nmRxDataFld.R[3]
#define nmRxDataFldByteUser2            nmRxDataFld.R[4]
#define nmRxDataFldByteUser3            nmRxDataFld.R[5]
#define nmRxDataFldByteUser4            nmRxDataFld.R[6]
#define nmRxDataFldByteUser5            nmRxDataFld.R[7]

/* nmTxDataFld */                 

#define nmTxDataFldOpCodeMsgType        nmTxDataFld.B.OpCodeMsgType
#define nmTxDataFldOpCodeSleepInd       nmTxDataFld.B.OpCodeSleepInd
#define nmTxDataFldOpCodeSleepAck       nmTxDataFld.B.OpCodeSleepAck

#define nmTxDataFldByteDestID           nmTxDataFld.B.DestID
#define nmTxDataFldByte0                nmTxDataFld.R[0]
#define nmTxDataFldByte1                nmTxDataFld.R[1]
#define nmTxDataFldByteUser0            nmTxDataFld.R[2]
#define nmTxDataFldByteUser1            nmTxDataFld.R[3]
#define nmTxDataFldByteUser2            nmTxDataFld.R[4]
#define nmTxDataFldByteUser3            nmTxDataFld.R[5]
#define nmTxDataFldByteUser4            nmTxDataFld.R[6]
#define nmTxDataFldByteUser5            nmTxDataFld.R[7]

 
#endif


/*******************************************************************************
* Name:    nmAlarmType
* Used by: NmSetAlarm() (parameter) 
*******************************************************************************/

typedef enum
{
    /* nmAlarm=0, Timer TTyp */
    NM_TIMER_TYP = 0, 
    /* nmAlarm=1, Timer TMax */
    NM_TIMER_MAX = 1, 
    /* nmAlarm=2, Timer TErr */
    NM_TIMER_ERR = 2,
    /* nmAlarm=3, Timer TWbs */
    NM_TIMER_WBS = 3, 
    /* nmAlarm=4, Timer TTx */
    NM_TIMER_TX = 4,  
    /* nmAlarm=5, fast Timer Tbusoff,OsekNm_006*/
    NM_FAST_TIMER_TBUSOFF = 5,  
    /* nmAlarm=6, Slow Timer Tbusoff,OsekNm_008*/
    NM_SLOW_TIMER_TBUSOFF = 6,
    #if(T_ACTIVE_MINI_ON == STD_ON)
	/* nmAlarm=7, Timer Tactivemini */ /*ziliang_debug*/
    NM_TIMER_ACTIVEMINI  = 7,
	#endif
	#if(T_LIMPHOME_MINI_ON == STD_ON) 
    /* nmAlarm=8, Timer TLimpHome */   /*ziliang_debug*/
    NM_TIMER_LIMPHOME    = 8 
	#endif             
} nmAlarmType;


/*******************************************************************************
* Name:    nmInitModeType
* Used by: NmInit() (parameter) 
*******************************************************************************/
typedef enum
{
    /* SDL Enter0: NM first Init ---> OsekDirNmInit() and NmStart()*/
    NM_D_BUSINIT = 0,  
    /* SDL Enter1: from bus off or normal */
    NM_D_BUSOFF = 1, 
    /* SDL Enter2: NMInitBusSleep to NMTwbsNormal From ---> 
    NMNormal/NMNormalPreSleep to NMTwbsNormal*/
    NM_D_NORMAL_TO_BUSSLEEP = 2, 
    /* SDL Enter3: leave NMBusSleep ---> NM state is sleep and GotoMode(Awake) 
    is called or WakeUp signal is received*/
    NM_D_BUSAWAKE = 3,  
     /* SDL Enter4: do not Init NM */
    NM_D_NOTINIT = 4, 
    /* SDL Enter5: leave NMLimpHome */
    NM_D_LIMPHOME = 5, 
    /* SDL Enter6: leave NMNormal */
    NM_D_NORMAL = 6,  
    /* SDL Enter7: NMInitBusSleep to NMTwbsLimpHome */
    NM_D_LIMPHOME_TO_BUSSLEEP = 7 
} nmInitModeType;

/*******************************************************************************
* Name:    nmSleepAwakeModeType
* Used by: GotoMode() (parameter) 
*******************************************************************************/
typedef enum
{
    GOTO_BUSSLEEP = 0,
    GOTO_AWAKE = 1
} nmSleepAwakeModeType;

/*******************************************************************************
* Name:    nmStateType
* Used by: NmGetCurState() (return type)
*          CanNm_SetNxtState() (parameter)
* Description:
*******************************************************************************/
typedef uint8 nmStateType;

#if defined ( NM_TYPE_YKXH )

/*******************************************************************************
* Name:        nmIndexType
* Description: This type is used for the Index counter.
* Note:        
*******************************************************************************/
typedef uint8 nmIndexType;
#endif
/*******************************************************************************
* Name:    NmReturnType
* Used by: CmpConfig() (return type) 
*******************************************************************************/
typedef uint8 nmReturnType;

typedef enum{
    NM_E_OK,
    NM_E_NOT_OK,
    NM_E_NOT_EXECUTED
}Nm_ReturnType;

/* Structure for post-build channel configuration parameters. */
typedef struct
{
  PduIdType CANNM_ID;                      /* L-PDU handle of NM PDUs to be transmitted. */

} OsekNm_PbChannelConfigType;

typedef P2CONST(OsekNm_PbChannelConfigType, CANNM_PBCFG, CANNM_PBCFG )  OsekNm_PbChannelConfigPtrType;


/* Structure for (post-build able) CAN NM configuration parameters */
typedef struct
{
  /* Post-build Channel Configuration */
  OsekNm_PbChannelConfigPtrType OsekNm_PbChannelConfigPtr;
} OsekNm_ConfigType;

extern uint8 ConfigMaskTableAry[NM_NUMBER_OF_CHANNELS][NM_NUMBER_OF_CONFIG_NODES];

extern uint8 TargetConfigTableAry[NM_NUMBER_OF_CHANNELS][NM_NUMBER_OF_CONFIG_NODES];


#define NM_MSG_TYPE_NORMAL        0
#define NM_MSG_TYPE_LIMPHOME      1

#define NM_NODE_STATE_NORMAL      0
#define NM_NODE_STATE_LIMPHOME    1

/*******************************************************************************
  Global variable prototypes
*******************************************************************************/
extern uint8 gNm_SleepFlag;
extern uint8 gNmBusoffFlag;
extern uint8 gNmBusOffConfirmFlag;
extern uint8 gCanNm_CanRxAppFlag ;
extern uint8 gCanNm_CanTxAppFlag;
/*******************************************************************************
  Global function prototypes
*******************************************************************************/
/*API NM function for Application*/
extern void CanNm_Init(const OsekNm_ConfigType* const cannmConfigPtr );
extern void CanNm_Talk(NetworkHandleType nmChannelHandle);
extern void CanNm_Silent(NetworkHandleType nmChannelHandle);
extern void CanNm_Start(NetworkHandleType nmChannelHandle);
extern void CanNm_Stop(NetworkHandleType nmChannelHandle);
extern void CanNm_GotoMode
    (NetworkHandleType nmChannelHandle,nmSleepAwakeModeType modeSleepAwake);
extern void CanNm_MainFunction(NetworkHandleType nmChannelHandle);
extern void CanNm_BusOff(NetworkHandleType nmChannelHandle);
extern void CanNm_RxIndication(PduIdType canNmPduId,
    Can_IdType canNmRxId, PduInfoType* pCanNmRxPduPtr);
extern void CanNm_TxConfirmation(PduIdType canNmTxPduId);
extern void CanNm_WakeUp(NetworkHandleType nmChannelHandle);

extern Nm_ReturnType CanNm_GetUserData
    (NetworkHandleType nmChannelHandle,uint8 * const nmRingData);
extern Nm_ReturnType CanNm_SetUserData
    (NetworkHandleType nmChannelHandle,const uint8 * const nmRingData);

extern void CanNm_GetConfig(NetworkHandleType nmChannelHandle, uint8 * dataPtr);
extern uint8 CanNm_CmpConfig(NetworkHandleType nmChannelHandle, uint8 idx );

/*indicate nm normal*/
extern void ApplNmCanNormal(NetworkHandleType nmChannelHandle); 
/*This callback sets the communication hardware (CAN controller,bus transceiver) 
* to a state from which they can be woken up by the CAN bus.*/
extern void ApplNmCanSleep(NetworkHandleType nmChannelHandle);
/*indicates bus off error occured*/
extern void ApplNmBusOff(NetworkHandleType nmChannelHandle);
/*indicates can bus wake up*/
extern void ApplNmCanWakeUp(NetworkHandleType nmChannelHandle);
/*Enter LimpHome State,OsekNm_007*/
extern void ApplNmCanLimpHomeStart(NetworkHandleType nmChannelHandle);
extern void ApplNmLimpHomeState(NetworkHandleType nmChannelHandle);
/*Exit LimpHome State,OsekNm_007*/
extern void ApplNmCanLimpHomeEnd(NetworkHandleType nmChannelHandle);
/*Slow Bus Off recovery failed*/

extern void ApplNmCanBusOffRecoveryFail(NetworkHandleType nmChannelHandle);
/*indicate end of bus off error */
extern void ApplNmBusOffEnd(NetworkHandleType nmChannelHandle); 
/*This callback function is performed when busOff recovery successed.*/
extern void ApplNmBusOffRecoverySuccess(NetworkHandleType nmChannelHandle);
extern void ApplNmWaitBusSleepCancel(NetworkHandleType nmChannelHandle);
extern void ApplNmWaitBusSleep(NetworkHandleType nmChannelHandle);

extern nmStateType CanNm_GetCurState(void);



#endif  /* _OSEKNM_H_ */

/* End of File */
